
angular.module('theApp.servicesA', [])

.service('A', function() {

    return {
   
        send_email: function($http, $scope, to, from, subject, text) {
            
            var srv = "php/server.php";
            var x = {'op': 'email', 'data': {"to": to, "from": from, "subject": subject, "text": text}};            
            $http.post(srv, {'payload': x}).success(function(data, status, headers, config) {      
                 $scope.response = JSON.stringify(data);
            }).error(function(data, status, headers, config) {
            });
        },
        
        get_user_data_from_provider: function(root, $rootScope, $location, $route, authData) {
            var uid = authData.uid;
            var uRef = root.child('users').child(uid);
            uRef.once('value', function(snap) {
                var prov = authData.provider;
                var data = snap.val();
                var luser = authData;
                if (data === null) {
                    luser.email = '-- email --';
                    luser.permission = 'User';
                    luser.phone1 = '555-555-5555';
                    luser.phone2 = '555-555-5555';
                    luser.phone3 = '555-555-5555';
                    luser.phone4 = '555-555-5555';
                    luser.lastname = '-- last name --';
                    luser.firstname = '-- last name --';
                    uRef.set(luser);                                    
                } else {
                    luser.email = data.email;
                    luser.permission = data.permission;
                    luser.phone1 = data.phone1;
                    luser.phone2 = data.phone2;
                    luser.phone3 = data.phone3;
                    luser.phone4 = data.phone4;                                    
                    luser.lastname = data.lastname;
                    luser.firstname = data.firstname;
                }
                
                if (prov === 'facebook') {
                    luser.email = authData[prov].email;
                    luser.lastname = authData.facebook.cachedUserProfile.last_name;
                    luser.firstname = authData.facebook.cachedUserProfile.first_name;
                }
                else if (prov === 'google') {
                    luser.email = authData[prov].email;
                    luser.lastname = authData.google.cachedUserProfile.family_name;
                    luser.firstname = authData.google.cachedUserProfile.given_name;                    
                }
                else if (prov === 'twitter') {         
                    luser.lastname = authData.twitter.cachedUserProfile.name + ' --- ';
                    luser.firstname = authData.twitter.cachedUserProfile.name + ' --- ';
                }
                else if (prov === 'password') {         
                    luser.email = authData[prov].email;
                }

                uRef.update(luser);                                    
                            
                $rootScope.luser = luser;
                
                $location.path('/home');
                $route.reload();
            });
            
            $location.path('/home');
        }
    };
});

